<template>
  <div>
    <tableComponents :pagination="{
        currentPage,
        pageSize: pagesize,
        total: robot_list.all_num
      }" @handleCurrentChange="handleCurrentChange" @handleSizeChange="handleSizeChange" @sendUserSearch="receive_search"
        :searchData="userSearchData" :isHaveExport="false" @toAdd="add_robot">
      <template v-slot:table>
        <el-table ref="multipleTable" :data="robot_list.data" v-loading="loading" element-loading-text="拼命加载中" stripe
            header-row-class-name="head_color" tooltip-effect="dark" style="width: 99.8%" @selection-change="handleSelectionChange">
          <el-table-column type="selection"></el-table-column>

          <el-table-column label="回复内容" min-width="200">
            <template slot-scope="scope">{{ scope.row.content }}</template>
          </el-table-column>
          <el-table-column label="关键词" min-width="200">
            <template slot-scope="scope">{{ scope.row.keyword }}</template>
          </el-table-column>
          <el-table-column label="是否模糊搜索" min-width="200">
            <template slot-scope="scope">{{
              scope.row.is_like == 1 ? "是" : "否"
            }}</template>
          </el-table-column>

          <el-table-column label="创建时间" min-width="200">
            <template slot-scope="scope">{{
              scope.row.created_time | getTimeFormat
            }}</template>
          </el-table-column>

          <el-table-column label="修改时间" min-width="200">
            <template slot-scope="scope">{{
              scope.row.update_time | getTimeFormat
            }}</template>
          </el-table-column>

          <el-table-column label="操作" min-width="150">
            <template slot-scope="scope">
              <el-button :loading="$store.state.isLoading" size="mini" type="primary" @click="update_robot(scope.row.id)">编辑</el-button>
              <el-button :loading="$store.state.isLoading" size="mini" type="danger" @click="delete_robot(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:footerLeft>
        <a class="aBtn aBtn_1 aBtn_lan2" @click="deleteAll_robots">删除</a>
      </template>
    </tableComponents>

    <el-dialog :close-on-click-modal="false" title="添加机器人" v-loading="dioLoading" class="tl size_13" width="800px" :visible.sync="dialogaddVisible">
      <el-form :model="add_form" ref="form" :label-width="formLabelWidth">
        <el-form-item label="关键词：">
          <el-col :span="12">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="add_form.keyword" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="回复内容：">
          <el-col :span="12">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="add_form.content" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="是否模糊搜索：">
          <el-col :span="12">
            <el-select v-model="add_form.is_like" size="small" clearable placeholder="请选择" style="width: 100%">
              <el-option v-for="item in type_option" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button :loading="$store.state.isLoading" type="primary" @click="add_submit()">添 加</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" title="编辑机器人" v-loading="dioLoading" class="tl size_13" width="800px" :visible.sync="dialogFormVisible">
      <el-form :model="robot_single" ref="form" :label-width="formLabelWidth">
        <el-form-item label="关键词：">
          <el-col :span="12">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="robot_single.keyword" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="回复内容：">
          <el-col :span="12">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="robot_single.content" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="是否模糊搜索：">
          <el-col :span="12">
            <el-select v-model="robot_single.is_like" size="small" clearable placeholder="请选择" style="width: 100%">
              <el-option v-for="item in type_option" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button :loading="$store.state.isLoading" type="primary" @click="saveForm()">编 辑</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { robotSearchData } from "@/assets/js/searchData/searchData"; //引入搜索的数据
export default {
  name: "robot",
  data () {
    return {
      userSearchData: robotSearchData,
      add_form: {
        keyword: "",
        content: "",
        is_like: "0"
      },
      type_option: [
        {
          label: "否",
          value: "0"
        },
        {
          label: "是",
          value: "1"
        }
      ],
      robot_single: {
        id: "",
        keyword: "",
        created_time: "",
        update_time: "",
        content: "",
        is_like: ""
      },
      dialogaddVisible: false,
      dialogFormVisible: false,
      dioLoading: false,
      multipleSelection: [],
      robot_list: {
        data: []
      },
      loading: false,
      currentPage: 1, //初始页
      pagesize: 10, //    每页的数据
      serarch_from: {},
      formLabelWidth: "120px"
    };
  },
  created () {
    this.get_robot_list();
  },
  methods: {
    add_submit () {
      var params = this.add_form;
      this.$api.getim.add_robotlist(params).then(res => {
        if (res.data.code == 200) {
          this.$fnc.alertSuccess("添加成功");
          this.dialogaddVisible = false;
          this.get_robot_list();
        } else {
          this.$fnc.alertError(res.data.result);
          this.get_robot_list();
        }
      });
    },
    add_robot () {
      this.add_form = {
        keyword: "",
        content: "",
        is_like: "0"
      };
      this.edit_flag = true;
      this.dialogaddVisible = true;
    },
    saveForm () {
      var params = {};
      params.id = this.robot_single.id;
      params.keyword = this.robot_single.keyword;
      params.content = this.robot_single.content;
      params.is_like = this.robot_single.is_like;
      this.$api.getim.edit_robotlist(params).then(res => {
        if (res.data.code == 200) {
          this.$fnc.alertSuccess("修改成功");
          this.dialogFormVisible = false;
          this.get_robot_list();
        } else {
          this.$fnc.alertError(res.data.result);
          this.get_robot_list();
        }
      });
    },
    delete_robot (id) {
      //删除
      this.$swal({
        title: "您确定要删除这些信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          this.$api.getim.del_robotlist({ id_str: id }).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.get_robot_list();
            } else {
              this.$swal("删除失败！", "出错啦！", "success");
              this.get_robot_list();
            }
          });
        }
      });
    },
    update_robot (id) {
      //修改页面
      //   this.showImg = true;
      this.edit_flag = true;
      this.dialogFormVisible = true;
      this.$api.getim.find_robotlist({ id: id }).then(res => {
        if (res.data.code == 200) {
          this.robot_single = res.data.result;
        }
      });
    },
    deleteAll_robots (del_id) {
      //批量删除
      console.log(del_id);
      var str = "";
      if (this.multipleSelection.length <= 0) {
        this.$message.error("未选择批量删除");
      } else {
        for (var i in this.multipleSelection) {
          str += this.multipleSelection[i].id + "@";
        }
        str = str.slice(0, -1);
        this.$swal({
          title: "您确定要删除这些信息吗",
          text: "删除后将无法恢复，请谨慎操作！",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "是的，我要删除",
          cancelButtonText: "容我三思"
        }).then(willDelete => {
          if (willDelete.value) {
            this.$api.getim.del_robotlist({ id_str: str }).then(res => {
              if (res.data.code == 200) {
                this.$swal("删除成功！", "您已经删除了这条信息。", "success");
                this.get_robot_list();
              } else {
                this.$swal("删除失败！", "出错啦！", "success");
                this.get_robot_list();
              }
            });
          }
        });
      }
    },
    handleSelectionChange (val) {
      this.multipleSelection = val;
      console.log(val);
    },
    receive_search (form) {
      this.serarch_from = form;
      this.get_robot_list(form);
    },
    get_robot_list (data) {
      this.loading = true;
      var params = {};
      data = data || {};
      params = data;
      this.$api.getim.get_robotlist(params).then(res => {
        if (res.data.code == 200) {
          this.robot_list = res.data.result;
          this.loading = false;
        } else {
          this.$fnc.alerterror(res.data.result);
          this.loading = false;
        }
      });
    },
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function (size) {
      //每页下拉显示数据
      var params = this.serarch_from;
      params.page = "1";
      params.page_size = size;
      this.get_robot_list(params);
      this.pageSize = size;
    },
    handleCurrentChange: function (currentPage) {
      this.currentPage =  currentPage;

      var params = this.serarch_from;
      params.page = currentPage;
      params.page_size = this.pageSize;
      this.get_robot_list(params);
    }
  }
};
</script>
<style scoped></style>
